package io.renren.modules.job.task;

/**
 * Copyright 2018
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

import io.renren.common.utils.ConfigConstant;
import io.renren.common.utils.DateUtils;
import io.renren.common.utils.DecimalUtils;
import io.renren.modules.aic.entity.PriceBookEntity;
import io.renren.modules.aic.service.PriceBookService;
import io.renren.modules.sys.entity.SysConfigEntity;
import io.renren.modules.sys.service.SysConfigService;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

/**
 * 
 * @author Mark sunlightcs@gmail.com
 * @Description: 价格趋势任务
 * @since 1.2.0 2016-11-28
 */
@Component("priceBookTask")
public class PriceBookTask {
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	PriceBookService priceBookService;

	@Autowired
	SysConfigService sysConfigService;

	/**
	 * 更新市场信息
	 */
	public void updatePriceBook() {
		logger.info("更新市场信息");
		double maxPrice = sysConfigService.getValueD(ConfigConstant.ORDER_MAX_PRICE);
		double rise = sysConfigService.getValueD(ConfigConstant.PRICE_UP_RISE);
		int randomMaxVol = sysConfigService.getValueI(ConfigConstant.RANDOM_MAX_VOL);
		double min = DecimalUtils.mul(maxPrice, 0.97);
		double price = maxPrice;
		Date now = new Date();
		Date startOfDay = DateUtils.getStartOfDay(now);
		if (now.before(DateUtils.addDateHours(startOfDay, 20))) {
			price = DecimalUtils.nextDouble(min, maxPrice, 2);
		}
		Double op = price;
		Double lp = min;
		PriceBookEntity lastPB = priceBookService.selectOne(new EntityWrapper<PriceBookEntity>().ge("create_time", startOfDay).orderBy("create_time", false).last("limit 0,1"));
		if (lastPB != null) {
			op = lastPB.getClosePrice();
			lp = min < lastPB.getLowPrice() ? min : lastPB.getLowPrice();
		}
		PriceBookEntity priceBook = new PriceBookEntity();
		priceBook.setOpenPrice(op);
		priceBook.setHighPrice(maxPrice);
		priceBook.setLowPrice(lp);
		priceBook.setClosePrice(price);
		priceBook.setVol((new Random().nextInt(randomMaxVol) + sysConfigService.getValueI(ConfigConstant.COIN_MIN_QTY)) + 0L);
		priceBook.setRise(rise);
		priceBook.setCreateTime(now);
		priceBookService.insert(priceBook);
	}

	/**
	 * 更新买单最高价格
	 */
	public void updateMaxPrice() {
		logger.info("更新买单最高价格");
		String rise = sysConfigService.getValue(ConfigConstant.PRICE_UP_RISE);
		SysConfigEntity sysConfig = sysConfigService.selectOne(new EntityWrapper<SysConfigEntity>().eq("`key`", ConfigConstant.ORDER_MAX_PRICE));
		String maxPrice = sysConfig.getValue();
		sysConfig.setValue(new BigDecimal("1").add(new BigDecimal(rise)).multiply(new BigDecimal(maxPrice)).setScale(2, BigDecimal.ROUND_DOWN).toString());
		// 如果保留两位小数后值未变就+0.01
		if (maxPrice.equals(sysConfig.getValue())) {
			sysConfig.setValue(new BigDecimal(maxPrice).add(new BigDecimal("0.01")).toString());
		}
		sysConfigService.update(sysConfig);
	}
}
